package com.hdg.boot.service.impl;

import com.alibaba.excel.EasyExcel;
import com.hdg.boot.bo.XirMdBo;
import com.hdg.boot.dto.ExcelAddDownloadDto;
import com.hdg.boot.dto.ExcelUpdateDownloadDto;
import com.hdg.boot.excel_listener.DataComparisonListener;
import com.hdg.boot.excel_listener.UploadListener;
import com.hdg.boot.mapper.XirMdMapper;
import com.hdg.boot.service.DataCompareService;
import com.hdg.boot.service.XirMdService;
import com.hdg.boot.util.excel_style.ExcelStyle01;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Service
public class XirMdServiceImpl implements XirMdService {

    public static final String TRD = "t_xir_trd";
    public static final String CRMS = "t_xir_crms";
    public static final String RISK = "t_xir_risk";
    public static final String MD = "t_xir_md";
    public static final String ADD = "add";
    public static final String UPDATE = "update";

    @Resource
    XirMdMapper xirMdMapper;
    @Resource
    DataCompareService dataCompareService;

    public void insert(){

    }

    @Override
    @Transactional
    public void addXirMd(List<XirMdBo> list) {
        //xirMdMapper.addXirMds(list);
    }


    @Override
    @Transactional
    public void upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String tableName = getTableName(fileName);
        EasyExcel.read(file.getInputStream(), XirMdBo.class, new UploadListener(xirMdMapper,tableName)).sheet().headRowNumber(1).doRead();
    }

    @Override
    public void dataComparison(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String tableName = getTableName(fileName);
        EasyExcel.read(file.getInputStream(), XirMdBo.class, new DataComparisonListener(dataCompareService,tableName)).sheet().headRowNumber(1).doRead();
    }

    @Override
    public void download(HttpServletResponse response, String type) throws IOException{
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(fileName(type), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        if(type.equals(UPDATE)){
            List<ExcelUpdateDownloadDto> list = dataCompareService.getUpdateData();
            EasyExcel.write(response.getOutputStream(), ExcelUpdateDownloadDto.class).sheet("sheet").doWrite(list);
        }else {
            List<ExcelAddDownloadDto> list = dataCompareService.getAddData();
            EasyExcel.write(response.getOutputStream(), ExcelAddDownloadDto.class).sheet("sheet").doWrite(list);
        }

    }

    private String getTableName(String fileName){
        if(fileName.toLowerCase().contains("trd")){
            return TRD;
        }else if(fileName.toLowerCase().contains("crms")){
            return CRMS;
        }else if(fileName.toLowerCase().contains("risk")){
            return RISK;
        }else if(fileName.toLowerCase().contains("md")){
            return MD;
        }else {
            throw new RuntimeException("该表不存在!");
        }
    }

    private String fileName(String type){
        if(type.equals(UPDATE)){
            return "表数据结构改变统计";
        }else if (type.equals(ADD)){
            return "增加表统计";
        }else{
            throw new RuntimeException("type 类型值只能输入 update与ad两种");
        }
    }
}
